package com.example.delay.job;

import com.example.delay.listener.JobListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/*
 * @className: RunJob
 * @author: chentao
 * @date: 2025/2/20 上午11:06
 * @Version: 1.0
 * @description:
 */
//任务的代理对象
public class RunJob implements Runnable, Serializable {

    Logger logger = LoggerFactory.getLogger(getClass());

    private String jobId;

    private Class jobClass;

    private JobListener jobListener;

    public RunJob(String jobId, Class jobClass, JobListener jobListener) {
        this.jobId = jobId;
        this.jobClass = jobClass;
        this.jobListener = jobListener;
    }

    @Override
    public void run() {
        try {
            DelayJob delayJob = (DelayJob) jobClass.newInstance();
            jobListener.before(jobClass, jobId);
            delayJob.exec(jobClass, jobId);
        } catch (Exception e) {
            logger.error("任务执行失败: " + e.getMessage());
        } finally {
            jobListener.after(jobClass, jobId);
        }
    }
}
